package models;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.Id;

import com.avaje.ebean.*;

import play.db.ebean.Model;
import play.db.ebean.Model.Finder;

@Entity
public class edu_tags extends Model{
	
	@Id
	public int tag_id;
	
	public String tag;
			
	public long creation_date;
	
	public static Finder<Long,edu_tags> find = new Finder<Long,edu_tags>(Long.class, edu_tags.class);	
	
	public static edu_tags findByName(String tag) {
        return edu_tags.find.where().eq("tag", tag).findUnique();
	}
	
	public static List<edu_tags> findByCourseID(String course_id)
	{
		String sql = "select distinct t.tag_id,t.tag from edu_tags t join edu_tagscourses etc on t.tag_id=etc.tag_id where etc.course_id="+course_id;
		//String sql = "select id, name from customer where name like :name and status_code = :status";
		SqlQuery sqlQuery = Ebean.createSqlQuery(sql);
		
		ArrayList<edu_tags> etl = new ArrayList<edu_tags>();
		
		// execute the query returning a List of MapBean objects
		List<SqlRow> list = sqlQuery.findList();
		for (SqlRow r : list)
		{
			edu_tags et = new edu_tags();
			et.tag = r.getString("tag");
			et.tag_id = r.getInteger("tag_id");
			etl.add(et);
		}
		return etl;
	}
	
		
}
